package com.thomas.sdk.manager;

import java.io.File;
import java.io.FileOutputStream;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ClipboardManager;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.media.MediaScannerConnection;
import android.media.MediaScannerConnection.OnScanCompletedListener;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.Images;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

@SuppressLint("NewApi")
public class Utils {
	/**
	 * 获取和保存当前屏幕的截图
	 */
	@SuppressWarnings("deprecation")
	public static void GetandSaveCurrentImage(Activity ctx) {
		// 构建Bitmap
		WindowManager windowManager = ctx.getWindowManager();
		Display display = windowManager.getDefaultDisplay();
		int w = display.getWidth();
		int h = display.getHeight();
		Bitmap Bmp = Bitmap.createBitmap(w, h, Config.ARGB_8888);
		// 获取屏幕
		View decorview = ctx.getWindow().getDecorView();
		decorview.setDrawingCacheEnabled(true);
		Bmp = decorview.getDrawingCache();
		// 图片存储路径
		String SavePath = getSDCardPath() + "/beclub/";
		// 保存Bitmap
		try {
			File path = new File(SavePath);
			// 文件
			String filepath = SavePath + DateManager.formatDateTime() + ".png";
			File file = new File(filepath);
			if (!path.exists()) {
				path.mkdirs();
			}
			if (!file.exists()) {
				file.createNewFile();
			}
			FileOutputStream fos = null;
			fos = new FileOutputStream(file);
			if (null != fos) {
				Bmp.compress(Bitmap.CompressFormat.PNG, 90, fos);
				fos.flush();
				fos.close();
				Toast.makeText(ctx, "截屏图片已保存到图库的beclub文件夹中", Toast.LENGTH_LONG)
						.show();
			}
			mediaScan(ctx, file);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取SDCard的目录路径功能
	 * 
	 * @return
	 */
	public static String getSDCardPath() {
		return Environment.getExternalStorageDirectory().getPath();
	}

	/**
	 * 复制文字到剪贴板
	 * 
	 * @param message
	 *            文字的内容
	 * @param context
	 *            上下文对象
	 */
	@SuppressWarnings("deprecation")
	public static void copyStrToClipboard(String message, Context context) {
		// 获取剪贴板管理服务
		ClipboardManager cm = (ClipboardManager) context
				.getSystemService(Context.CLIPBOARD_SERVICE);
		// 将文本数据复制到剪贴板
		cm.setText(message);
	}

	/**
	 * 让Gallery上能马上看到该图片
	 */
	public static void scanPhoto(final Context ctx, String imgFileName) {
		File f = new File(imgFileName);
		Uri contentUri = Uri.fromFile(f);
		Intent mediaScanIntent = new Intent(
				Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, contentUri);
		ctx.sendBroadcast(mediaScanIntent);
	}

	/**
	 * 第二种扫描指定图片的方法
	 * 
	 * @param ctx
	 * @param file
	 */
	public static void mediaScan(Context ctx, File file) {
		MediaScannerConnection.scanFile(ctx,
				new String[] { file.getAbsolutePath() }, null,
				new OnScanCompletedListener() {
					@Override
					public void onScanCompleted(String path, Uri uri) {
						Log.v("MediaScanWork", "file " + path
								+ " was scanned seccessfully: " + uri);
					}
				});
	}

	/**
	 * 将图片插入相册
	 * 
	 * @param filePath
	 * @param context
	 */
	public static void addImageToGallery(final String filePath,
			final Context context) {

		ContentValues values = new ContentValues();

		values.put(Images.Media.DATE_TAKEN, System.currentTimeMillis());
		values.put(Images.Media.MIME_TYPE, "image/jpeg");
		values.put(MediaStore.MediaColumns.DATA, filePath);

		context.getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI,
				values);
	}

	public static void galleryAddPic(File image, Context ctx) {
		Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_MOUNTED);
		String mCurrentPhotoPath = image.getAbsolutePath();
		File file = new File(mCurrentPhotoPath);
		Uri contentUri = Uri.fromFile(file);
		mediaScanIntent.setData(contentUri);
		ctx.sendBroadcast(mediaScanIntent);
	}
}
